<template>
  <ele-page>
    <!-- 搜索表单 -->
    <file-search @search="reload"/>
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="id"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        v-model:selections="selections"
        :highlight-current-row="true"
        :export-config="{ fileName: '就业统计-就业数据文件' }"
        cache-key="statisticFileTable"
        :loadOnCreated="false"
      >
        <template #toolbar>
          <el-button
            type="primary"
            class="ele-btn-icon"
            :icon="UploadOutlined"
            @click="openImport()"
          >
            导入
          </el-button>
        </template>
        <template #action="{ row }">
          <el-link
            type="primary"
            underline="never"
            :icon="ExpandOutlined"
            @click="openPanel(row)"
          >
            统计面板
          </el-link>
          <el-divider
            direction="vertical"
          />
          <el-dropdown @command="(cmd) => handleCommand(cmd,row)" style="margin-top: 5px;">
            <el-link
              type="primary"
              underline="never"
              :icon="InsertRowOutlined"
            >
              其它
            </el-link>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="handleContrast">
                  <el-icon>
                    <ReloadOutlined/>
                  </el-icon>
                  比对
                </el-dropdown-item>
                <el-dropdown-item command="handleExport">
                  <el-icon>
                    <DownloadOutlined/>
                  </el-icon>
                  下载
                </el-dropdown-item>
                <el-dropdown-item command="removeBatch">
                  <el-icon>
                    <DeleteOutlined/>
                  </el-icon>
                  删除
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </template>
        <template #fileName="{ row }">
          <el-link
            type="primary"
            :icon="TableOutlined"
            @click="openPanel(row)"
          >
            &nbsp;{{ row.fileName }}
          </el-link>
        </template>
      </ele-pro-table>
    </ele-card>
    <file-import v-model="showImport"></file-import>
    <file-panel v-model="showPanel" :data="current"></file-panel>
  </ele-page>
</template>


<script setup>
import {ref} from 'vue';
import {
  PlusOutlined,
  DeleteOutlined,
  DownloadOutlined,
  UploadOutlined,
  ReloadOutlined,
  TableOutlined,
  InsertRowOutlined,
  ExpandOutlined
} from '@/components/icons';
import {ElMessageBox} from 'element-plus';
import {EleMessage} from 'ele-admin-plus';
import FileSearch from './components/file-search.vue';
import {pageFiles, removeFiles} from '@/api/manage/statistic/admin/fileInfo';
import {selectCurrentYear} from '@/api/manage/statistic/admin/schoolInfo';
import FileImport from './components/file-import.vue';
import FilePanel from "./components/file-panel.vue";
import {
  download,  // 用于下载文件流
} from '@/utils/common';
import request from '@/utils/request';


defineOptions({name: 'StatisticFile'});

/** 表格实例 */
const tableRef = ref(null);

/** 表格列配置 */
const columns = ref([
  {
    prop: 'fileName',
    label: '文件名',
    align: 'center',
    minWidth: 110,
    slot: 'fileName'
  },
  {
    prop: 'fileTime',
    label: '上传时间',
    align: 'center',
    minWidth: 110
  },
  {
    prop: 'fileYear',
    label: '毕业年份',
    align: 'center',
    minWidth: 90
  },
  {
    prop: 'studentCount',
    label: '学生数',
    align: 'center',
    minWidth: 90
  },
  {
    prop: 'signingCount',
    label: '签约人数',
    align: 'center',
    minWidth: 90
  },
  {
    prop: 'signingRate',
    label: '签约率',
    align: 'center',
    minWidth: 90
  },
  {
    prop: 'employmentCount',
    label: '就业人数',
    align: 'center',
    minWidth: 90
  },
  {
    prop: 'employmentRate',
    label: '就业率',
    align: 'center',
    minWidth: 90
  },
  {
    columnKey: 'action',
    label: '操作',
    width: 270,
    align: 'center',
    slot: 'action',
    hideInPrint: true,
    hideInExport: true
  }
]);

/** 表格选中数据 */
const selections = ref([]);

/** 当前编辑数据 */
const current = ref(null);

/** 是否显示编辑弹窗 */
// const showEdit = ref(false);

/**
 * 是否显示就业面板弹窗
 */
const showPanel = ref(false);

/** 表格数据源 */
const datasource = ({pages, where}) => {
  return pageFiles({...where, ...pages});
};

/** 搜索 */
const reload = (where) => {
  tableRef.value?.reload?.({page: 1, where});
};

/** 批量删除 */
const removeBatch = (row) => {
  const rows = row == null ? selections.value : [row];
  if (!rows.length) {
    EleMessage.error({message: '请至少选择一条数据', plain: true});
    return;
  }
  ElMessageBox.confirm(
    `是否确认删除文件名为"${rows.map((d) => d.fileName).join()}"的数据项?`,
    '系统提示',
    {type: 'warning', draggable: true}
  )
    .then(() => {
      const loading = EleMessage.loading({
        message: '请求中..',
        plain: true
      });
      removeFiles(rows.map((d) => d.id))
        .then(() => {
          loading.close();
          EleMessage.success({message: '删除成功', plain: true});
          reload();
        })
        .catch((e) => {
          loading.close();
          EleMessage.error({message: e.message, plain: true});
        });
    })
    .catch(() => {
    });
};


const showImport = ref(false);

const openImport = () => {
  showImport.value = true;
}

const handleContrast = async (row) => {
  const res = await request({
    url: '/admin/employment/statistic/fileInfo/contrast/' + row.id,
    method: 'POST',
    data: {},
    responseType: 'blob'
  });
  //console.log(res);
  download(res.data, `student_contrast_${(new Date().toLocaleDateString()) + (new Date().toLocaleTimeString())}.xlsx`);
}

const handleExport = async (row) => {
  const res = await request({
    url: '/admin/employment/statistic/fileInfo/download/' + row.id,
    method: 'POST',
    data: {},
    responseType: 'blob'
  });
  download(res.data, '全国毕业生就业管理系统数据_' + row.fileTime + '.xlsx');
}

const handleShowReport = (row) => {

}

/**
 * 表格下拉框
 * @param command
 * @param row
 */
const handleCommand = (command, row) => {
  console.log(command, row);
  switch (command) {
    case "handleContrast":
      handleContrast(row);
      break;
    case "handleExport":
      handleExport(row);
      break;
    case "removeBatch":
      removeBatch(row);
      break;
    default:
      break;
  }
}


const openPanel = (row) => {
  showPanel.value = true;
  current.value = row;
}

</script>
